package org.dromara.erp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.erp.domain.ErpFarmSaleInfo;
import org.dromara.erp.mapper.ErpFarmSaleInfoMapper;
import org.dromara.erp.service.IErpFarmSaleInfoService;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 销售明细Service业务层处理
 *
 * @author Mr Lei QQ:235921049
 * @date 2024-06-16
 */
@RequiredArgsConstructor
@Service
public class ErpFarmSaleInfoServiceImpl implements IErpFarmSaleInfoService {

    private final ErpFarmSaleInfoMapper baseMapper;

    /**
     * 查询销售明细
     */
    @Override
    public ErpFarmSaleInfo queryById(Long id){
        return baseMapper.selectById(id);
    }

    /**
     * 查询销售明细列表
     */
    @Override
    public TableDataInfo<ErpFarmSaleInfo> queryPageList(ErpFarmSaleInfo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ErpFarmSaleInfo> lqw = buildQueryWrapper(bo);
        Page<ErpFarmSaleInfo> result = baseMapper.selectPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询销售明细列表
     */
    @Override
    public List<ErpFarmSaleInfo> queryList(ErpFarmSaleInfo bo) {
        LambdaQueryWrapper<ErpFarmSaleInfo> lqw = buildQueryWrapper(bo);
        return baseMapper.selectList(lqw);
    }

    private LambdaQueryWrapper<ErpFarmSaleInfo> buildQueryWrapper(ErpFarmSaleInfo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ErpFarmSaleInfo> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getOrderId() != null, ErpFarmSaleInfo::getOrderId, bo.getOrderId());
        lqw.eq(bo.getCateId() != null, ErpFarmSaleInfo::getCateId, bo.getCateId());
        lqw.eq(bo.getProductId() != null, ErpFarmSaleInfo::getProductId, bo.getProductId());
        return lqw;
    }

    /**
     * 新增销售明细
     */
    @Override
    public Boolean insertByBo(ErpFarmSaleInfo bo) {
        ErpFarmSaleInfo add = MapstructUtils.convert(bo, ErpFarmSaleInfo.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改销售明细
     */
    @Override
    public Boolean updateByBo(ErpFarmSaleInfo bo) {
        ErpFarmSaleInfo update = MapstructUtils.convert(bo, ErpFarmSaleInfo.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ErpFarmSaleInfo entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除销售明细
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
